<?php
/**
 * Created by PhpStorm.
 * User: prg
 * Date: 2017/3/26
 * Time: 23:55
 */

require_once 'db.php';
require_once 'response.php';

header('content-type:text/html;charset:utf-8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

$id = isset($_POST['id']) ? $_POST['id'] : '';
//$id = $_GET['id'];

if(!is_numeric($id)) {
    return Response::show(401,'数据不合法');
}

try {
    $connect = Db::getInstance()->connect();
} catch ( Exception$e) {
    return Response::show(403,'数据库连接失败',$e->getMessage());
}

// 数据库中字段删除之后，再进行服务器文件的删除，但是要先拿到文件的路径，不然删除了就找不到了
$sql = "SELECT team_img FROM team WHERE team_id={$id}";
$res = $connect->query($sql);

if($res && $res->num_rows>0) {
    $url = $res->fetch_row();
}
$res->free_result();

$sql = "DELETE FROM team WHERE team_id={$id}";
$res = $connect->query($sql);

$data = array(
    'IsDelete' => false
);

if($res) {
    if($connect->affected_rows == 1) {
        $data['IsDelete'] = true;
        @unlink("./".$url[0]);
        Response::show(200,'团队信息删除成功',$data);
        exit();
    } else {
        return Response::show(404,'找不到此id的团队',$data);
    }
} else {
    return Response::show(402,'sql语句执行不成功',$data);
}

